import request from "@/utils/request";

/**
 * 获取网络配置
 * @returns {Promise<Object>} 网络配置信息
 */
export async function getNetworkConfig() {
  try {
    const response = await request.post("/v1/jsonrpc2", {
      method: "network.getConfig",
      params: {}
    });
    return response.data;
  } catch (error) {
    console.error("获取网络配置失败:", error);
    throw error;
  }
}

/**
 * 设置网络为DHCP模式
 * @param {Object} params 配置参数
 * @param {string} params.interface_name 网络接口名称
 * @param {boolean} [params.enable=true] 是否启用网卡
 * @returns {Promise<Object>} 配置结果
 */
export async function configureNetworkDhcp(params) {
  try {
    const response = await request.post("/v1/jsonrpc2", {
      method: "network.setConfig",
      params: {
        interface_name: params.interface_name,
        enable: params.enable !== undefined ? params.enable : true,
        mode: "dhcp"
      }
    });
    return response.data;
  } catch (error) {
    console.error("配置DHCP网络失败:", error);
    throw error;
  }
}

/**
 * 设置网络为静态IP模式
 * @param {Object} params 配置参数
 * @param {string} params.interface_name 网络接口名称
 * @param {boolean} [params.enable=true] 是否启用网卡
 * @param {Object} params.ipv4 IPv4配置
 * @param {string} params.ipv4.address IP地址
 * @param {string} params.ipv4.netmask 子网掩码
 * @param {string} params.ipv4.gateway 网关地址
 * @returns {Promise<Object>} 配置结果
 */
export async function configureNetworkStatic(params) {
  try {
    const response = await request.post("/v1/jsonrpc2", {
      method: "network.setConfig",
      params: {
        interface_name: params.interface_name,
        enable: params.enable !== undefined ? params.enable : true,
        mode: "static",
        ipv4: {
          address: params.ipv4.address,
          netmask: params.ipv4.netmask,
          gateway: params.ipv4.gateway
        }
      }
    });
    return response.data;
  } catch (error) {
    console.error("配置静态IP网络失败:", error);
    throw error;
  }
}

/**
 * 更新DNS服务器配置
 * @param {Array<string>} dnsServers DNS服务器列表
 * @returns {Promise<Object>} 配置结果
 */
export async function updateDnsServers(dnsServers) {
  try {
    const response = await request.post("/v1/jsonrpc2", {
      method: "network.updateDNS",
      params: {
        dns: dnsServers
      }
    });
    return response.data;
  } catch (error) {
    console.error("更新DNS服务器失败:", error);
    throw error;
  }
}

/**
 * 配置网络（统一方法）
 * @param {Object} params 网络配置参数
 * @param {string} params.interface_name 网络接口名称
 * @param {string} params.mode 网络模式: 'dhcp'/'static'
 * @param {boolean} [params.enable=true] 是否启用网卡
 * @param {Object} [params.ipv4] IPv4配置 (静态IP模式时必须)
 * @param {string} [params.ipv4.address] IP地址
 * @param {string} [params.ipv4.netmask] 子网掩码
 * @param {string} [params.ipv4.gateway] 网关地址
 * @param {Array<string>} [params.ipv4.dns] DNS服务器列表
 * @returns {Promise<Object>} 配置结果
 */
export async function configureNetwork(params) {
  try {
    const requestParams = {
      interface_name: params.interface_name,
      enable: params.enable !== undefined ? params.enable : true,
      mode: params.mode
    };

    if (params.mode === "static" && params.ipv4) {
      requestParams.ipv4 = {
        address: params.ipv4.address,
        netmask: params.ipv4.netmask,
        gateway: params.ipv4.gateway
      };

      // 如果提供了DNS配置，添加到IPv4配置中
      if (params.ipv4.dns && Array.isArray(params.ipv4.dns)) {
        requestParams.ipv4.dns = params.ipv4.dns;
      }
    } else if (params.mode === "dhcp" && params.ipv4 && params.ipv4.dns) {
      // DHCP模式下，如果提供了DNS配置，也需要添加
      requestParams.ipv4 = {
        dns: params.ipv4.dns
      };
    }

    const response = await request.post("/v1/jsonrpc2", {
      method: "network.setConfig",
      params: requestParams
    });
    return response.data;
  } catch (error) {
    console.error("配置网络失败:", error);
    throw error;
  }
}